import Vue from 'vue'
import VueRouter from 'vue-router'


const Login = () => import('../views/login/login.vue')
const Layout = () => import('../views/layout/layout.vue')

const Article = () => import('../views/articles/article.vue')
const Image = () => import('../views/images/image.vue')
const Add = () => import('../views/addArticle/add')
const Comment = () => import('../views/comment/comment')
const Fans = () => import('../views/fans/fans')
const Settings = () => import('../views/settings/settings')

const Page404 = () => import('../views/page404/page404')
const Edit = () => import('../views/articles/edit')

Vue.use(VueRouter)

// 路由配置表
const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/articles', component: Article },
      { path: '/image', component: Image },
      { path: '/addArticle', component: Add },
      { path: '/comment', component: Comment },
      { path: '/fans', component: Fans },
      { path: '/settings', component: Settings },
      { path: '/edit/:id', component: Edit }
    ]
  },
  { path: '*', component: Page404 }
]


// 配置路由规则
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(Date.now(), '路由跳转')
  console.log(to, from, next)
  // 如果你要去的页面是 "VIP" 页面
  const pageList = ['/', '/image', '/articles', '/addArticle']
  // if (to.path === '/image' || to.path === '/article') {
  if (pageList.includes(to.path)) {
    // 1. 获取token
    const tokenStr = localStorage.getItem('tokenStr')
    if (tokenStr) {
      next() // 放行
    } else {
      console.log('没有token')
      next('/login')
    }
  } else {
    next() // 放行
  }
})

export default router
